/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
import hasAuth from "@/utils/permission/hasAuth.ts";
import MaUploadImage from "@/components/ma-upload-image/index.vue";
import { concat } from "lodash-es";
import MaRemoteSelect from "@/components/ma-remote-select/index.vue";
import { page as commodityTypePage } from "$/attractions/commerce/api/commerceCommodityType.ts";
import { page as merchantPage } from "$/attractions/commerce/api/commerceMerchant.ts";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: any
): MaFormItem[] {
  return concat([
    {
      label: "商品名称",
      prop: "commodity_name",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "商品名称" }) },
      itemProps: { rules: [{ required: true, message: "请输入商品名称" }] },
    },
    {
      label: "商品类型",
      prop: "commodity_category",
      render: () => MaRemoteSelect,
      renderProps: {
        api: commodityTypePage,
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((item: any) => ({
              label: item.name || `商品类型${item.id}`,
              value: item.id,
            }));
          }
          return [];
        },
        placeholder: "请选择商品类型",
        clearable: true,
        multiple: false,
      },
      itemProps: { rules: [{ required: true, message: "请选择商品类型" }] },
    },
    {
      label: "商户名称",
      prop: "merchant_id",
      render: () => MaRemoteSelect,
      renderProps: {
        api: merchantPage,
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((item: any) => ({
              label: item.merchant_name || `商户名称${item.id}`,
              value: item.id,
            }));
          }
          return [];
        },
        placeholder: "请选择商户名称",
        clearable: true,
        multiple: false,
      },
      itemProps: { rules: [{ required: true, message: "请选择商户名称" }] },
    },
    {
      label: "商品品牌",
      prop: "commodity_brand",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "商品品牌" }) },
    },
    {
      label: "商品型号",
      prop: "commodity_model",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "商品型号" }) },
    },
    {
      label: "商品规格",
      prop: "commodity_spec",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "商品规格" }) },
    },
    {
      label: "计量单位",
      prop: "commodity_unit",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "计量单位" }) },
      itemProps: { rules: [{ required: true, message: "请输入计量单位" }] },
    },
    {
      label: "商品描述",
      prop: "commodity_description",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "商品描述" }) },
    },
    {
      label: "销售价（元）",
      prop: "sale_price",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "销售价" }) },
    },
    {
      label: "折扣",
      prop: "discount",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "折扣" }) },
      itemProps: { rules: [{ required: true, message: "请输入折扣" }] },
    },
    {
      label: "满减金额",
      prop: "full_amount",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "满减金额" }) },
    },
    {
      label: "满减额度",
      prop: "full_reduction",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "满减额度" }) },
    },
    {
      label: "库存数量",
      prop: "stock_quantity",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "库存数量" }) },
      itemProps: { rules: [{ required: true, message: "请输入库存数量" }] },
    },
    {
      label: "商品图片",
      prop: "commodity_images",
      render: () => MaUploadImage,
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "商品图片" }),
      },
      multiple: true,
    },
    {
      label: "排序",
      prop: "sort_order",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "排序" }) },
      itemProps: { rules: [{ required: true, message: "请输入排序" }] },
    },
  ]);
}
